package com.farm.farming.service.impl;

import com.farm.entity.PageResult;
import com.farm.farming.dao.Cust_roleMapper;
import com.farm.farming.pojo.Cust_role;
import com.farm.farming.service.Cust_roleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Id;
import java.util.List;

@Service
@Transactional
public class Cust_roleServiceImpl implements Cust_roleService {

    @Autowired
    private Cust_roleMapper cust_roleMapper;

    @Override
    public int add(Cust_role cust_role,Integer[] menuIds,Integer[] permissionIds) {

        Cust_role role = cust_roleMapper.save(cust_role);

        if (role!=null){
            //保存给角色添加的菜单权限的数据
            saveMenuIdsAndPermissionIds(role.getId(),menuIds,permissionIds);
        }

        return 1;
    }

    private void saveMenuIdsAndPermissionIds(Integer id, Integer[] menuIds, Integer[] permissionIds) {
        if (menuIds!=null && menuIds.length!=0){
            //添加角色和菜单的中间表
            for (Integer menuId : menuIds) {
                cust_roleMapper.saveRoleIdAndMenuId(id,menuId);
            }
        }
        if (permissionIds!=null && permissionIds.length!=0){
            //添加角色和权限的中间表
            for (Integer permissionId : permissionIds) {
                cust_roleMapper.saveRoleIdAndPermissionId(permissionId,id);
            }
        }
    }

    @Override
    public int delete(Integer id) {

        //先查询用户和角色的中间表有没有数据有就不能删除
        int count = cust_roleMapper.findTcusturByroleId(id);
        if (count!=0){
            throw  new RuntimeException("删除角色失败");
        }
        //再删除角色和权限的中间表
        cust_roleMapper.deleteTcustRolePermissionByroleId(id);
        //删除角色和菜单的中间表
        cust_roleMapper.deleteTcustRoleMenuByroleId(id);
        //删除角色的数据
        cust_roleMapper.deleteById(id);
        return 1;
    }

    @Override
    public int update(Cust_role cust_role) {
        Integer[] permissionIds = cust_role.getPermissionIds();
        Integer[] menuIds = cust_role.getMenuIds();

        //修改角色的数据
        Cust_role role = cust_roleMapper.save(cust_role);
        Integer id = role.getId();
        //删除角色和权限中间表数据再添加
        cust_roleMapper.deleteTcustRolePermissionByroleId(id);

        //删除角色和菜单中间表数据
        cust_roleMapper.deleteTcustRoleMenuByroleId(id);
        //调用save方法添加中间表的数据
        this.saveMenuIdsAndPermissionIds(id,menuIds,permissionIds);
        return 1;
    }

    @Override
    public List<Cust_role> findAll() {
        return cust_roleMapper.findAll();
    }

    @Override
    public PageResult<Cust_role> findPage(Cust_role cust_role,Integer page,Integer size) {
        //添加条件
        if (cust_role==null)
        cust_role = new Cust_role();

        Example<Cust_role> example = Example.of(cust_role, ExampleMatcher.matching()
                .withMatcher("role", ExampleMatcher.GenericPropertyMatchers.contains()));

        //分页查询
        PageRequest pageRequest = PageRequest.of(page, size);
        Page<Cust_role> all = cust_roleMapper.findAll(example, pageRequest);
        return new PageResult<>(all.getTotalElements(),all.getContent());

    }

    @Override
    public Cust_role findById(Integer id) {

        return cust_roleMapper.getOne(id);
    }

}
